package juc.thread.threadPool;

import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/*
*
* */
class Test8{
    public static void main(String[] args) {
        ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1);
     /*   scheduledExecutorService.schedule(()->{
            System.out.println("task1");
            try {
                Thread.sleep(3000);
            } catch (Exception e) {
                e.printStackTrace();
            }
            int i=1/0;
        },1, TimeUnit.SECONDS);
        scheduledExecutorService.schedule(()->{
            System.out.println("task2");
            try {
                Thread.sleep(3000);
            } catch (Exception e) {
                e.printStackTrace();
            }
            int i=1/0;
        },1, TimeUnit.SECONDS);*/
      /*  scheduledExecutorService.scheduleAtFixedRate(()->{
            System.out.println("周期任务...");
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },1,1,TimeUnit.SECONDS);*/
        scheduledExecutorService.scheduleWithFixedDelay(()->{
            System.out.println("周期任务...");
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },1,3,TimeUnit.SECONDS);
    }
}
/*
* 测试任务调度器 jdk5 Timer
* 多个任务由一个线程执
* 当某个任务出现未能捕获的异常后 后续任务不能执行
* */
public class Test7 {
    public static void main(String[] args) throws InterruptedException {
        Timer timer=new Timer();
        TimerTask task1= new TimerTask() {
            @Override
            public void run() {
                System.out.println("task1");
                try {
                    Thread.sleep(3000);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                int i=1/0;
            }
        };
        TimerTask task2= new TimerTask() {
            @Override
            public void run() {
                System.out.println("task2");
            }
        };
        timer.schedule(task1,1000);
        timer.schedule(task2,1000);
        Thread.sleep(5000);
        System.out.println("main...");
    }
}
